<?php
namespace App\Controller;

class DealPointsController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：获取优惠信息
    * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        $deal_points = $this->DealPoints->findById($id)->first();
        if(empty($deal_points)){
            $this->G->error("deal_point_error");
            return null;
        }
        $deal_points['worth'] = $deal_points['worth']/100;
        $deal_points['begin_time'] = date('Y-m-d H:i:s', $deal_points['begin_time']);
        $deal_points['end_time'] = date('Y-m-d H:i:s', $deal_points['end_time']);
        $this->G->success("success", $deal_points);
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：删除优惠
    * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        $deal_points = $this->DealPoints->find()
            ->select(['id'])
            ->where(['id in' => $ids])
            ->toArray();
        if(!empty($deal_points)){
            $this->loadComponent('DealPointCache');
            foreach ($deal_points as $k=>$v){
                $this->DealPointCache->deleteCache($v['id']);
            }
            $this->DealPoints->deleteAll(['id in'=>$ids]);
        }
        $this->G->success("success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：优惠列表
    * */
    public function getList()
    {
        $title = $this->request->getData('title');
        $page = $this->request->getData('page');
        $where = [];
        if(!empty($title)){
            $where['title like '] = '%'.$title.'%';
        }
        $alllist = $page == 'all' ? true : false;
        $page = (empty($page) || $page == 'all') ? 1 :$page;
        if($alllist){
            $deal_points = $this->DealPoints->find()
                ->where($where)
                ->order(['worth desc']);
        }else{
            $deal_points = $this->DealPoints->find()
                ->where($where)
                ->order(['worth desc'])
                ->limit($this->_page_num)
                ->page($page);
        }
        $re = [];
        $count = $deal_points->count();
        $re['pageTotal'] = ceil($count/$this->_page_num);
        $deal_points = $deal_points->toArray();
        $re['list'] = [];
        if(!empty($deal_points)){
            foreach ($deal_points as $k=>$v){
                $v['worth'] = $v['worth']/100;
                $v['begin_time'] = date('Y-m-d H:i:s', $v['begin_time']);
                $v['end_time'] = date('Y-m-d H:i:s', $v['end_time']);
                $re['list'][] = $v;
            }
        }
        $this->G->success("Success", $re);
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：修改优惠
    * */
    public function update()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        foreach ($data as $k=>$v){
            if($k == 'requirement' || $k == 'worth' || $k == 'user_group_id'){
                continue;
            }elseif(empty($v)){
                $this->G->error($k."_error");
                return null;
            }
        }
        $deal_points = $this->DealPoints->findById($data['id'])->select(['id'])->first();
        if(empty($deal_points)){
            $this->G->error("deal_point_error");
            return null;
        }
        $deal_point = $this->DealPoints->newEntity();
        $deal_point->id = $data['id'];
        $deal_point->title = $data['title'];
        $deal_point->requirement = empty($data['requirement']) ? 0 : $data['requirement'];
        $deal_point->worth = empty($data['worth']) ? 0 : $data['worth']*100;
        $deal_point->user_group_id = empty($data['user_group_id']) ? 0 : $data['user_group_id'];
        $deal_point->begin_time = strtotime($data['begin_time']);
        $deal_point->end_time = strtotime($data['end_time']);
        $this->DealPoints->save($deal_point);
        $this->loadComponent('DealPointCache');
        $this->DealPointCache->deleteCache($data['id']);
        $this->G->success("Success");
    }

    /*
    * create by phpstorm
    * author：Ming
    * date：2020.04.30
    * ps：新增优惠
    * */
    public function add()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        foreach ($data as $k=>$v){
            if($k == 'requirement' || $k == 'worth' || $k == 'user_group_id'){
                continue;
            }elseif(empty($v)){
                $this->G->error($k."_error");
                return null;
            }
        }
        $deal_point = $this->DealPoints->newEntity();
        $deal_point->title = $data['title'];
        $deal_point->requirement = empty($data['requirement']) ? 0 : $data['requirement'];
        $deal_point->worth = empty($data['worth']) ? 0 : $data['worth']*100;
        $deal_point->user_group_id = empty($data['user_group_id']) ? 0 : $data['user_group_id'];
        $deal_point->begin_time = strtotime($data['begin_time']);
        $deal_point->end_time = strtotime($data['end_time']);
        $this->DealPoints->save($deal_point);
        $this->G->success("Success");
    }

}
?>